package com.atguigu.gmall.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.gmall.bean.PmsProductImage;
import com.atguigu.gmall.bean.PmsProductInfo;
import com.atguigu.gmall.bean.PmsProductSaleAttr;
import com.atguigu.gmall.bean.PmsProductSaleAttrValue;
import com.atguigu.gmall.manager.mapper.PmsProductImageMapper;
import com.atguigu.gmall.manager.mapper.PmsProductInfoMapper;
import com.atguigu.gmall.manager.mapper.PmsProductSaleAttrMapper;
import com.atguigu.gmall.manager.mapper.PmsProductSaleAttrValueMapper;
import com.atguigu.gmall.service.SpuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SpuServiceImpl implements SpuService {
    @Autowired
    PmsProductInfoMapper pmsProductInfoMapper;

    @Autowired
    PmsProductImageMapper pmsProductImageMapper;

    @Autowired
    PmsProductSaleAttrMapper pmsProductSaleAttrMapper;
    @Autowired
    PmsProductSaleAttrValueMapper pmsProductSaleAttrValueMapper;

    @Override
    public List<PmsProductInfo> spuList(String catalog3Id) {
        PmsProductInfo p = new PmsProductInfo();
        p.setCatalog3Id(catalog3Id);
        return pmsProductInfoMapper.select(p);
    }

    @Override
    public boolean saveSpuInfo(PmsProductInfo pmsProductInfo) {
        if(StringUtils.isNotBlank(pmsProductInfo.getSpuName())){
            pmsProductInfo.setProductName(pmsProductInfo.getSpuName());
        }
        ///保存商品信息
        pmsProductInfoMapper.insertSelective(pmsProductInfo);
        //生成商品主键
        String productId = pmsProductInfo.getId();
        //保存商品图片信息
        List<PmsProductImage> spuImageList = pmsProductInfo.getSpuImageList();
        for (int i = 0; i < spuImageList.size() ; i++) {
            PmsProductImage p = spuImageList.get(i);
            p.setProductId(productId);
            pmsProductImageMapper.insertSelective(p);
        }
        ///保存销售属性信息
        List<PmsProductSaleAttr> spuSaleAttrList = pmsProductInfo.getSpuSaleAttrList();
        for (int i = 0; i < spuSaleAttrList.size(); i++) {
            PmsProductSaleAttr p= spuSaleAttrList.get(i);
            p.setProductId(productId);
            pmsProductSaleAttrMapper.insertSelective(p);

            //保存销售属性值
            List<PmsProductSaleAttrValue> spuSaleAttrValueList = p.getSpuSaleAttrValueList();
            for (int j = 0; j < spuSaleAttrValueList.size() ; j++) {
                PmsProductSaleAttrValue pv = spuSaleAttrValueList.get(j);
                pv.setProductId(productId);
                pmsProductSaleAttrValueMapper.insert(pv);
            }
        }
        return true;
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrList(String spuId) {
        PmsProductSaleAttr p = new PmsProductSaleAttr();
        p.setProductId(spuId);
        List<PmsProductSaleAttr> pmsProductSaleAttrs = pmsProductSaleAttrMapper.select(p);
        for (int i = 0; i < pmsProductSaleAttrs.size(); i++) {
            PmsProductSaleAttr pmsProductSaleAttr = pmsProductSaleAttrs.get(i);
            PmsProductSaleAttrValue pv = new PmsProductSaleAttrValue();
            pv.setProductId(spuId);
            pv.setSaleAttrId(pmsProductSaleAttr.getSaleAttrId());
            List<PmsProductSaleAttrValue> select = pmsProductSaleAttrValueMapper.select(pv);
            pmsProductSaleAttr.setSpuSaleAttrValueList(select);
        }
        return pmsProductSaleAttrs;
    }

    @Override
    public List<PmsProductImage> spuImageList(String spuId) {
        PmsProductImage p = new PmsProductImage();
        p.setProductId(spuId);
        return pmsProductImageMapper.select(p);
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrListCheckBySku(String productId,String skuId) {
        List<PmsProductSaleAttr> pmsProductSaleAttrs = pmsProductSaleAttrMapper.selectSpuSaleAttrListCheckBySku(productId, skuId);
        return pmsProductSaleAttrs;
    }
}
